#!/bin/bash

CATTLE_URL=${CATTLE_URL:-http://localhost:8081}

wait_a_bit()
{
    echo -n "."
    sleep 6
}

for ((i=0;i<300;i++)); do
    if [[ "$(curl -s ${CATTLE_URL}/ping)" = "pong" ]]; then
        host_count=$(curl -s ${CATTLE_URL}/v3/hosts|python -mjson.tool|grep '"id"'|grep "1h.*"|wc -l)
        if [ "$host_count" -ge "1" ]; then
            echo "OK"
            break
        else
            wait_a_bit
        fi
    else
        wait_a_bit
    fi

    ### If we don't get a response after 2 mins... give up.
    if [ "$i" -gt "20" ]; then
        echo "Failed to reach environment" 1>&2
        echo "Rancher latest environment failed to respond... no tests run." 1>&2
        exit 1
    fi
done
